package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.wms.domain.WmsStockPosition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsStockPositionMapper;
import com.ruoyi.wms.domain.WmsStockPosition;
import com.ruoyi.wms.service.IWmsStockPositionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 仓位Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-02-24
 */
@Service
public class WmsStockPositionServiceImpl implements IWmsStockPositionService 
{
    @Resource
    private WmsStockPositionMapper wmsStockPositionMapper;

    /**
     * 查询仓位
     * 
     * @param stockPosUuid 仓位主键
     * @return 仓位
     */
    @Override
    public WmsStockPosition selectWmsStockPositionByStockPosUuid(String stockPosUuid)
    {
        return wmsStockPositionMapper.selectWmsStockPositionByStockPosUuid(stockPosUuid);
    }

    /**
     * 查询仓位列表
     * 
     * @param wmsStockPosition 仓位
     * @return 仓位
     */
    @Override
    public List<WmsStockPosition> selectWmsStockPositionList(WmsStockPosition wmsStockPosition)
    {
        return wmsStockPositionMapper.selectWmsStockPositionList(wmsStockPosition);
    }

    /**
     * 新增仓位
     * 
     * @param wmsStockPosition 仓位
     * @return 结果
     */
    @Override
    public int insertWmsStockPosition(WmsStockPosition wmsStockPosition)
    {
        wmsStockPosition.setCreateTime(DateUtils.getNowDate());
        return wmsStockPositionMapper.insertWmsStockPosition(wmsStockPosition);
    }

    /**
     * 修改仓位
     * 
     * @param wmsStockPosition 仓位
     * @return 结果
     */
    @Override
    public int updateWmsStockPosition(WmsStockPosition wmsStockPosition)
    {
        wmsStockPosition.setUpdateTime(DateUtils.getNowDate());
        return wmsStockPositionMapper.updateWmsStockPosition(wmsStockPosition);
    }

    /**
     * 批量删除仓位
     * 
     * @param stockPosUuids 需要删除的仓位主键
     * @return 结果
     */
    @Override
    public int deleteWmsStockPositionByStockPosUuids(String[] stockPosUuids)
    {
        return wmsStockPositionMapper.deleteWmsStockPositionByStockPosUuids(stockPosUuids);
    }

    /**
     * 删除仓位信息
     * 
     * @param stockPosUuid 仓位主键
     * @return 结果
     */
    @Override
    public int deleteWmsStockPositionByStockPosUuid(String stockPosUuid)
    {
        return wmsStockPositionMapper.deleteWmsStockPositionByStockPosUuid(stockPosUuid);
    }

    @Override
    @Transactional
    public String importItem(List<WmsStockPosition> stockPositionList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(stockPositionList) || stockPositionList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空或导入模板不正确！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (WmsStockPosition stockPosition : stockPositionList)
        {
            try
            {
                // 验证是否存在这个物料
                WmsStockPosition i = wmsStockPositionMapper.findStockPosCode(stockPosition.getStockPosCode());
                if (StringUtils.isNull(i))
                {
                    stockPosition.setCreateBy(operName);
                    this.insertWmsStockPosition(stockPosition);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、仓位编码 " + stockPosition.getStockPosCode() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    stockPosition.setUpdateBy(operName);
                    this.updateWmsStockPosition(stockPosition);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、仓位编码 " + stockPosition.getStockPosCode() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、仓位编码 " + stockPosition.getStockPosCode() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、仓位编码 " + stockPosition.getStockPosCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
